<?php
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 9-28/0028
 * Time: 15:33
 * y.12301.cc订单查询
 */

$salerids       = array_keys(SalerList());
$salerid        = abs($_GET['salerid']);
if (!in_array($salerid, $salerids)) {
    exit('{"code":100}');
}

/**
 *
 * REQUEST PARAMS
 *  begintime 开始时间
 *  endtime   结束时间
 *  xtimes    时间类型
 *  ordernum  订单号
 *  status    订单状态
 *  otel      手机号
 *  page      当前页数
 */

$soap           = SoapInit::GetSoapInside();
//$pOrder         = saleProOrderObject($_SESSION['memberID'], $soap, 2, $salerid);

$page           = $_REQUEST["page"]?intval($_REQUEST["page"]):1;
$pageSize       = 15;
$offset         = ($page-1)*$pageSize;


$bt  = empty($_GET['begintime']) ? date('Y-m-d') : $_GET['begintime'];
$et  = empty($_GET['endtime'])   ? date('Y-m-d') : $_GET['endtime'];
$ordernum = safetxt($_GET['ordernum']);
$status = '';
$otel = trim($_GET['otel']);
if (!empty($_GET['begintime']) ) {
    if (!checkData($bt)) {
        exit('{"code":103, "msg":"开始时间格式错误(正确格式:YYYY-MM-dd,如1989-08-08)"');
    }
    $bt = $_GET['begintime'];
} else {
    $bt = date('Y-m-d');
}
if (!empty($_GET['endtime']) ) {
    if (!checkData($et)) {
        exit('{"code":103, "msg":"结束时间格式错误(正确格式:YYYY-MM-dd,如1989-08-08)"');
    }
    $et = $_GET['endtime'];
} else {
    $et = date('Y-m-d');
}

if (!empty($ordernum) && !is_numeric($ordernum)) {
    exit('{"code":103, "msg":"订单号格式错误"');
}
if (!empty($otel) && !ismobile($otel) ) {
    exit('{"code":103,"msg":"手机号不正确"}');
}
$concat_id = 0;//关联订单(0显示所有订单 1显示关联订单【订单号不能为空】）
$if_pack   = 0;
$payStatus = '0|1';//支付状态（0景区到付|1成功|2未支付）

//$bt = date('Y-m-d');
$orderXml = $soap->Order_Globle_Search("{$salerid}","","","","","","","","$bt",
    "$et","","", $ordernum,"","$otel","","{$payStatus}","","","",$offset,
    $pageSize,0,"","","","","","","","",$concat_id,$if_pack);

$orderArr = simplexml_load_string($orderXml);

//print_r($orderXml);
//335530
$cnt      = count($orderArr);
$output   = array();
$statusList =array(
    "0"=>"未使用",
    "1"=>"已使用",
    "2"=>"已过期",
    "3"=>"已取消",
    "4"=>"凭证码被替代",
    "6"=>"被终端撤销(已取消)",
    "5"=>"被终端修改----(已使用)",
);

//echo $cnt;
if ($cnt==0) {
    $total     = 0;
    $totalPage = 0;
}
elseif ($cnt==1) {
    $total     = 1;
    $totalPage = 1;
    $row = array(
        'ordernum'  => (string)$orderArr->Rec->UUordernum,
        'name'      => (string)$orderArr->Rec->UUordername,
        'mobile'    => (string)$orderArr->Rec->UUordertel,
        'time'      => (string)$orderArr->Rec->UUbegintime,
        'ifpack'    => (int)$orderArr->Rec->UUifpack,
        'title'     => (string)$orderArr->Rec->UUttitle,
        'num'       => (int)$orderArr->Rec->UUtnum,
        //'pid'       => (int)$orderArr->Rec->UUpid,
        'tid'       => (int)$orderArr->Rec->UUtid,
        'status'    => (int)$orderArr->Rec->UUstatus,
    );
    if(
        date('Y-m-d')>=(string)$orderArr->Rec->UUbegintime && //在游玩日期内
        date('Y-m-d')<=(string)$orderArr->Rec->UUendtime &&
        $row['status'] == 0 &&//且未使用  可以验证
        ((int)$orderArr->Rec->UUconcat_id == 0  || //不是联票
            (int)$orderArr->Rec->UUconcat_id == (int)$orderArr->Rec->UUordernum // 联票的主票
        )
    )
    {
        $row['chk']=1;
    }
    if(
        date('Y-m-d')>=(string)$orderArr->Rec->UUbegintime && //在游玩日期内
        date('Y-m-d')<=(string)$orderArr->Rec->UUendtime &&
        $row['status'] == 0 && //且未使用  可以验证
        ((int)$orderArr->Rec->UUconcat_id != 0  && //是联票
            (int)$orderArr->Rec->UUconcat_id != (int)$orderArr->Rec->UUordernum // 不是联票的主票
        )
    )
    {
        $row['chk']=2;
    }
    $output[] = $row;
}
else {
    $total = $soap->Order_Globle_Search("{$salerid}","","","","","","","","$bt",
        "$et","","",$ordernum,"","$otel","","{$payStatus}","","","",$offset,
        $pageSize,1,"","","","","","","","",$concat_id,$ifpack);
    $totalPage  = ceil($total/$pageSize);
//    echo 'total' . $total;
    $tmp = $arr = $tickets = array();
//    $orders = $orderArr->Rec;
//    print_r($orderArr);exit;
    foreach($orderArr as $key=>$order) {
        $row = array();
        $row = array(
            'ordernum'  => (string)$order->UUordernum,
            'name'      => (string)$order->UUordername,
            'mobile'    => (string)$order->UUordertel,
            'time'      => (string)$order->UUbegintime,
            'ifpack'    => (int)$order->UUifpack,
            'title'     => (string)$order->UUttitle,
            'num'       => (int)$order->UUtnum,
            //'pid'       => (int)$order->UUpid,
            'tid'       => (int)$order->UUtid,
            'status'    => (int)$order->UUstatus,
        );
        if(
            date('Y-m-d')>=(string)$order->UUbegintime && //在游玩日期内
            date('Y-m-d')<=(string)$order->UUendtime &&
            $row['status'] == 0 && //且未使用  可以验证
            ((int)$order->UUconcat_id == 0  || //不是联票
                (int)$order->UUconcat_id == (int)$order->UUordernum // 联票的主票
            )
        )
        {
            $row['chk']=1;
        }
        if(
            date('Y-m-d')>=(string)$order->UUbegintime && //在游玩日期内
            date('Y-m-d')<=(string)$order->UUendtime &&
            $row['status'] == 0 && //且未使用  可以验证
            ((int)$order->UUconcat_id != 0  && //是联票
                (int)$order->UUconcat_id != (int)$order->UUordernum // 不是联票的主票
            )
        )
        {
            $row['chk']=2;
        }
        $output[] = $row;
    }
}
if ($_GET['t']) {
    print_r($output);
}
echo json_encode(array(
    'code'=>200,
    'list'=>$output,
    'total'=>$total,
    'totalpage'=>$totalPage,
    )
);